﻿﻿<form id="frmBXJGWorkOrderCategory" class="frm-container">
    <input type="hidden" name="id" value="0" />
    <table class="frm-box">
        <tr>
            <th>上级</th>
            <td><input name="parentId" value="0" /></td>
        </tr>
        <tr>
            <th>名称</th>
            <td><input name="displayName" class="easyui-textbox" tabindex="0" data-options="required:true,validType:'length[0,50]'" /></td>
        </tr>
        <tr>
            <th>关联工单类型</th>
            <td><input name="switchWorkOrderType" /></td>
        </tr>
        <tr class="workOrderTypesBox" style="display:none;">
            <th></th>
            <td style="width:250px;">
            </td>
        </tr>
        <tr class="workOrderTypesBox">
            <th></th>
            <td style="width:250px;"><input class="easyui-checkbox" name="isDefault" value="true" label="设为默认" /></td>
        </tr>
    </table>
</form>
<script>

    (function () {
        const m = 'BXJGWorkOrderCategory';
        const $dlg = $('#dlg' + m);
        const $frm = $('form');
        const $cbParent = $frm.find("[name='parentId']");
        const $switchWorkOrderType = $frm.find("[name='switchWorkOrderType']");
        const $wotbox0 = $frm.find('.workOrderTypesBox').eq(0);
        const $box = $wotbox0.find('td');
        const $wotbox1 = $frm.find('.workOrderTypesBox').eq(1);

        const id = $dlg.data('id');
        const parent = $dlg.data('parent');

        let initParentTask = $.Deferred();
        let initFormTask = $.Deferred();
        let initWorkOrderTypesTask = $.Deferred();


        //关联工单类型相关--------------开始---------------------------------
        $switchWorkOrderType.switchbutton({
            offText: '共享的',
            onText: '指定的',
            width: 95,
            value: true,
            onChange: function (checked) {
                if (checked) {
                    $wotbox0.show();
                    $wotbox1.hide();
                } else {
                    $wotbox0.hide();
                    $wotbox1.show();
                }
            }
        });

        //初始化关联类型
        initWorkOrderTypesTask = abp.services.bxjgworkorder.bXJGWorkOrderType.getAll({}).done(function (data) {
            data.forEach(function (curr, index) {
                const $div = $('<div style="border-bottom:1px solid #ddd; padding:8px 0;">').appendTo($box);
                $('<input bjName=' + curr.value + ' class="easyui-checkbox" value="' + curr.value + '" label="' + curr.displayText + '" />').appendTo($div);
                $('<input bjIsDefault=' + curr.value + ' class="easyui-checkbox" value="true" label="设为默认" />').appendTo($div);
            });
            $.parser.parse($box);
        });

        //设置关联工单类型的值
        function setWorkOrderTypes(data) {

            $box.find('input[bjName]').checkbox('uncheck');
            $box.find('input[bjIsDefault]').checkbox('uncheck');

            data.forEach(function (curr, index) {
                $box.find("input[bjName='" + curr.workOrderType + "']").checkbox('check');
                if (curr.isDefault)
                    $box.find("input[bjIsDefault='" + curr.workOrderType + "']").checkbox('check');
            });
        }
        //获取关联的工单类型的值
        function getWorkOrderTypes() {
            let ary = new Array();
            let $types = $box.find('input[bjName]');
        
            $types.each(function (index, curr) {
                let $curr = $(curr);
              
                if (!$curr.next().hasClass('checkbox-checked'))
                    return;

                let lx = $curr.attr('bjName');
                let item = { workOrderType: lx };
                item.isDefault = $box.find("input[bjIsDefault='" + lx + "']").next().hasClass('checkbox-checked');
                ary.push(item);
            });
            return ary;
        }
        //关联工单类型相关--------------结束---------------------------------


        //初始化父节点下拉框
        $cbParent.combotree({
            url: abp.appPath + 'api/services/bxjgworkorder/WorkOrderCategoryProvider/GetTreeForSelect',
            queryParams: { forType: 4/*, parentText: '全部'*/ },
            //prompt: abp.localization.bxjgUtils('全部'),
            onLoadSuccess: function (node, data) {
                initParentTask.resolve();
            },
            onLoadError: function () {
                initParentTask.reject();
            }
            //,loadFilter: function (data, parent) {
            //    data = $.fn.combotree.defaults.loadFilter(data, parent);
            //    if (data && data[0])
            //        data[0].id = 0;//奇怪这里用'0' 与输入口默认值对应不起来
            //    return data;
            //}
        });
        //$workOrderType.combobox({
        //    url: abp.appPath + 'api/services/bxjgworkorder/bxjgWorkOrdertype/getall',
        //    queryParams: { forType: 4 },
        //    editable:false
        //});

        $dlg.data('clickSave', function () {
            if (!$frm.form('validate'))
                return;
            let postData = $frm.serializeJSON(/*{ checkboxUncheckedValue: "false" }*/);
            if ($wotbox1.is(':hidden'))
                postData.workOrderTypes = getWorkOrderTypes();

            let defer;
            if (id) {
                defer = abp.services.bxjgworkorder.workOrderCategory.update(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('修改') + abp.localization.bxjgUtils('成功') + '!');
                });
            } else {
                postData.id = 0;
                defer = abp.services.bxjgworkorder.workOrderCategory.create(postData).done(function (d) {
                    abp.notify.success(abp.localization.bxjgUtils('新增') + abp.localization.bxjgUtils('成功') + '!');
                });
            }
            defer.done(function () {
                onSaved();
            });
            abp.ui.setBusy($dlg, defer);
        });

        function onSaved() {
            var f = $dlg.data('onSaved');
            if (f)
                f();
        }

        //没必要等下拉加载完再赋值，因为easyui会在加载完后自动处理已存在的值，其它控件也一样
        if (id) {
            initFormTask = abp.services.bxjgworkorder.workOrderCategory.get({ id: id }).done(function (d) {
                $frm.form('load', d);
                if (d.workOrderTypes && d.workOrderTypes.length > 0)
                    $switchWorkOrderType.switchbutton('check');
            });
        } else {
            $cbParent.combotree('setValue', parent);
            initFormTask.resolve();
        }

        abp.ui.setBusy($dlg, $.when(initFormTask, initParentTask, initWorkOrderTypesTask).done(function (d) {
            if (id) {
                setWorkOrderTypes(d[0].workOrderTypes);
            }
        }));

    })();
</script>
